package com.wuhao.code.check.ui

import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.components.PersistentStateComponent
import com.intellij.openapi.components.State
import com.intellij.openapi.components.Storage
import com.intellij.openapi.components.service

/**
 * 配置读取类
 * @author 吴昊
 * @since 1.0
 */
@State(
    name = "CodePluginSettings",
    storages = [Storage("codeplugin.xml")]
)
class PluginSettings : PersistentStateComponent<PluginSettings.State> {

  private var myState = State()

  var user: String
    get() = myState.user
    set(value) { myState.user = value }

  var maxLinesPerFile: String
    get() = myState.maxLinesPerFile
    set(value) { myState.maxLinesPerFile = value }

  var maxLinesPerFunction: String
    get() = myState.maxLinesPerFunction
    set(value) { myState.maxLinesPerFunction = value }

  companion object {
    val NULLABLE_INSTANCE: PluginSettings?
      get() = ApplicationManager.getApplication().getService(PluginSettings::class.java)
    
    val INSTANCE: PluginSettings
      get() = ApplicationManager.getApplication().service<PluginSettings>()
  }

  /**
   * 获取最大文件行数
   */
  fun getMaxLinesPerFileValue(): Int {
    return try {
      maxLinesPerFile.toInt()
    } catch (e: NumberFormatException) {
      1500
    }
  }

  /**
   * 获取最大方法行数
   */
  fun getMaxLinesPerFunctionValue(): Int {
    return try {
      maxLinesPerFunction.toInt()
    } catch (e: NumberFormatException) {
      100
    }
  }

  override fun getState(): State {
    return myState
  }

  override fun loadState(state: State) {
    myState = state
  }

  /**
   * 配置状态类
   */
  class State {
    var user: String = ""
    var maxLinesPerFile: String = "1500"
    var maxLinesPerFunction: String = "100"
  }
}

